from __future__ import annotations
from typing import Any

from .celery_app import app
from . import config as cfg
from .weather import geocode_city, fetch_today_daily, build_email_body
from .mailer import send_email


@app.task(name="app.tasks.send_daily_weather")
def send_daily_weather() -> dict[str, Any]:
    lat, lon, display_name = geocode_city(cfg.CITY_NAME)
    forecast = fetch_today_daily(lat, lon, cfg.TIMEZONE)
    subject = f"{forecast['date']} {display_name} 天气提醒"
    body = build_email_body(display_name, forecast)

    send_email(subject, body)

    return {
        "city": display_name,
        "forecast": forecast,
        "sent_to": cfg.EMAIL_TO,
    }


if __name__ == "__main__":
    # 便于本地快速测试
    print(send_daily_weather())